package com.dms;

import java.util.ArrayList;
import java.util.UUID;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DMSActivity extends Activity {

	private TextView mTextView;
	private Button mButton;
	
	ArrayList<Integer> gResponse = new ArrayList<Integer>();
	JSONObject requestBody;
	Boolean isSuccess = false, isNull = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	    	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mTextView = (TextView)findViewById(R.id.textView1);
		mButton = (Button)findViewById(R.id.button1);
		
		try
		{
			requestBody = new JSONObject();
			requestBody.put("UUID", getUUID());
			mTextView.setText(requestBody.toString());
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
		
		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				Intent intent = new Intent(DMSActivity.this, DMSCheckNetworkService.class);
				startService(intent);
				
				/*requestSlimAPI("http://222.98.224.104/Slim/DeviceState", requestBody);
				
				while(!isSuccess)
				{
					try
					{
						Thread.sleep(1000);
					}
					catch(InterruptedException e)
					{
						e.printStackTrace();
					}
				}
				
				if(isNull)
				{
					mTextView.setText("Null");
				}
				else
				{
					mTextView.setText(gResponse.get(0).toString());
					//Intent intent = new Intent(DMSActivity.this, DMSCheckDeviceStateService.class);
					Intent intent = new Intent(DMSActivity.this, DMSCheckNetworkService.class);
					startService(intent);
					//mTextView.setText("notNull");
				}*/
			}
			
		});
	}
	
	public void requestSlimAPI(final String url, final JSONObject requestBody)
	{
		Thread postThread = new Thread(new Runnable() {

			@Override
			public void run() {
				try
				{
					isNull = true;
					HttpClient httpClient = new DefaultHttpClient();
					httpClient.getParams().setParameter("http.protocol.expect-continue", false);
					httpClient.getParams().setParameter("http.connection.timeout", 15000);
					httpClient.getParams().setParameter("http.socket.timeout", 15000);
					
					HttpPost httpPost = new HttpPost(url);
					httpPost.setHeader("charset", "utf-8");
					httpPost.setHeader("Content-type", "application/json");
					//httpPost.setHeader("Connection", "close");
					
					StringEntity stringEntity = new StringEntity(requestBody.toString());
					httpPost.setEntity(stringEntity);
					
					HttpResponse httpResponse = httpClient.execute(httpPost);
					String responseBody = EntityUtils.toString(httpResponse.getEntity());
					
					Log.e("httpResponse", "httpResponse: " + httpResponse.getStatusLine());
					
					if (responseBody !=  null)
					{
						isNull = false;
						JSONObject jsonObject = new JSONObject(responseBody);
						
						gResponse.clear();
						gResponse.add(jsonObject.getInt("deviceState"));
					}
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				finally
				{
					isSuccess = true;
				}
			}
			
		});
		postThread.start();
	}
	
	public String getUUID()
	{
		final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);

        final String tmDevice, tmSerial, androidId;
        tmDevice = "" + tm.getDeviceId();
        tmSerial = "" + tm.getSimSerialNumber();
        androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

        UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
        String deviceId = deviceUuid.toString();
        
        deviceId = deviceId.replaceAll("-", "");
        
        return deviceId;
	}
	
}
